package com.wind.homework.tools;

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PaUrl {

    public static void main(String[] args) throws Exception {
        URL url = null;
        URLConnection urlconn = null;
        BufferedReader br = null;
//        PrintWriter pw = null;



        String regex = "/sthjt/c100456/2.*shtml";//url匹配规则

        Pattern p = Pattern.compile(regex);
        try {
            url = new URL("http://sthjt.sc.gov.cn/sthjt/c100456/zclistf.shtml");//爬取的网址
            urlconn = url.openConnection();
//            pw = new PrintWriter(new FileWriter("/Users/gaox/Desktop/url"), true);//将爬取到的链接存到本地文件
            br = new BufferedReader(new InputStreamReader(urlconn.getInputStream()));
            String buf = null;

            while ((buf = br.readLine()) != null) {
                Matcher buf_m = p.matcher(buf);

                while (buf_m.find()) {
                    String abc = "http://sthjt.sc.gov.cn"+buf_m.group();
                    System.out.println(abc);
                    PaHtml paHtml = new PaHtml();
                    paHtml.method(abc);

                    String sourceFileName = "/Users/gaox/Desktop/url/"+abc.substring(abc.length()-34,abc.length()-5)+"html";
                    String zipFileName = "/Users/gaox/Desktop/url/"+abc.substring(abc.length()-34,abc.length()-5)+"zip";

                    ZipUtil zipUtil = new ZipUtil();
                    zipUtil.zip(zipFileName, sourceFileName);


                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
//            pw.close();
        }


    }
}
